<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="优惠卷类型[0->全场赠券；1->会员赠券；2->购物赠券；3->注册赠券]" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="couponType">
              <a-input-number v-model="model.couponType" placeholder="请输入优惠卷类型[0->全场赠券；1->会员赠券；2->购物赠券；3->注册赠券]" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="优惠券图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="couponImg">
              <a-input v-model="model.couponImg" placeholder="请输入优惠券图片"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="优惠卷名字" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="couponName">
              <a-input v-model="model.couponName" placeholder="请输入优惠卷名字"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="num">
              <a-input-number v-model="model.num" placeholder="请输入数量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="amount">
              <a-input-number v-model="model.amount" placeholder="请输入金额" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="每人限领张数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="perLimit">
              <a-input-number v-model="model.perLimit" placeholder="请输入每人限领张数" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="使用门槛" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="minPoint">
              <a-input-number v-model="model.minPoint" placeholder="请输入使用门槛" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="startTime">
              <j-date placeholder="请选择开始时间" v-model="model.startTime"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endTime">
              <j-date placeholder="请选择结束时间" v-model="model.endTime"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="使用类型[0->全场通用；1->指定分类；2->指定商品]" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="useType">
              <a-input-number v-model="model.useType" placeholder="请输入使用类型[0->全场通用；1->指定分类；2->指定商品]" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="note">
              <a-input v-model="model.note" placeholder="请输入备注"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="发行数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="publishCount">
              <a-input-number v-model="model.publishCount" placeholder="请输入发行数量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="已使用数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="useCount">
              <a-input-number v-model="model.useCount" placeholder="请输入已使用数量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="领取数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="receiveCount">
              <a-input-number v-model="model.receiveCount" placeholder="请输入领取数量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="可以领取的开始日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="enableStartTime">
              <j-date placeholder="请选择可以领取的开始日期" v-model="model.enableStartTime"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="可以领取的结束日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="enableEndTime">
              <j-date placeholder="请选择可以领取的结束日期" v-model="model.enableEndTime"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="优惠码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="code">
              <a-input v-model="model.code" placeholder="请输入优惠码"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="可以领取的会员等级[0->不限等级，其他-对应等级]" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="memberLevel">
              <a-input-number v-model="model.memberLevel" placeholder="请输入可以领取的会员等级[0->不限等级，其他-对应等级]" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="发布状态[0-未发布，1-已发布]" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="publish">
              <a-input-number v-model="model.publish" placeholder="请输入发布状态[0-未发布，1-已发布]" style="width: 100%" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'SmsCouponForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/coupon/smsCoupon/add",
          edit: "/coupon/smsCoupon/edit",
          queryById: "/coupon/smsCoupon/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>